var gulp = require('gulp'),
	browserSync = require('browser-sync'),
	gulpLoadPlugins = require('gulp-load-plugins'),
	plugins = gulpLoadPlugins();

gulp.task('minify',function(){
	gulp.src('public/javascripts/*.js')
	.pipe(plugins.uglify())
	.pipe(gulp.dest('public/js'))
});


gulp.task('js',function(){
	return gulp.src('public/javascripts/*.js')
	.pipe(plugins.jshint())
	.pipe(plugins.jshint.reporter('default'))
	.pipe(plugins.uglify())
	.pipe(plugins.concat('app.js'))
	.pipe(gulp.dest('public/js'))
	
});

gulp.task('shell',plugins.shell.task(['npm start']));


//监测coffee文件变化
gulp.task('coffee',function(){
	gulp.src('public/coffee/*.coffee')
	.pipe(plugins.watch('public/coffee/*.coffee'))
	.pipe(plugins.coffee({bare:true}).on('error',plugins.util.log))
	.pipe(gulp.dest('public/js'))
	.pipe(plugins.livereload())
});

//监测less文件变化
gulp.task('less',function(){
	gulp.src('public/stylesheets/*.less')
	.pipe(plugins.watch('public/stylesheets/*.less'))
	.pipe(plugins.less())
	.pipe(gulp.dest('public/css'))
	.pipe(plugins.livereload())
});

//监测sass文件变化
gulp.task('sass',function(){
    gulp.src('public/sass/**/*.scss')
        .pipe(plugins.watch('public/sass/**/*.scss'))
        .pipe(plugins.sass({'outputStyle':'compressed'}))
        .pipe(gulp.dest('public/css'))
        .pipe(plugins.livereload());
});

//监测react变化
gulp.task('react',function(){
	gulp.src('public/react/**/*.jsx')
	.pipe(plugins.react())
	.pipe(gulp.dest('public/js'))
});

//browserSync 检测文件变化
gulp.task('serve',['less','sass','react','coffee','js'],function(){
	var config = {
		port:3002,
		proxy:'127.0.0.1:3030',
		files:['views/**/*.ejs','lib/**/*.js','routes/**/*.js','config/**/*.js']
	};
	
	browserSync(config);
	
	gulp.watch('./public/js/**/*.js',['js'],browserSync.reload());
	gulp.watch('./public/js/**/*.jsx',['react'],browserSync.reload());
	gulp.watch('./public/coffee/**/*.coffee',['coffee'],browserSync.reload());
	gulp.watch('./public/sass/**/*.scss',['sass'],browserSync.reload());
	gulp.watch('./public/stylesheets/**/*.less',['less'],browserSync.reload());
});

gulp.task('default',['shell','serve'],function(){
	
});